package com.china08.yxyapi.api.manage.sch.office.outernotice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;

/**
 * 学校管理员 外部通知 查询收件箱某一个收件详情查询
 * 
 */
@CrossOrigin
@RestController("SchOuterNoticeReceiveOneSrhController")
public class OuterNoticeReceiveOneSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/office/outerNotice/receive/oneSrh", method = RequestMethod.POST)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        // 当前教育局id
        String currentSchoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String currentUserId = getProfile(request, response).getId();

        // 查询当前消息
        // FIXED 业务库
        YxyNoticeOutter yxyNoticeOutter = yxyNoticeOutterRepository.findById(reqModel.id);
        List<YxyNotifictionSchool> yxyNotifictionSchooLst = yxyNoticeOutter.getReceiveSchool();
        for (YxyNotifictionSchool yxyNotifictionSchool : yxyNotifictionSchooLst) {
            if (currentSchoolId.equals(yxyNotifictionSchool.getSchoolId())) {
                yxyNotifictionSchool.setIsRead(1);
            }
        }
        // 点击阅读后更改为已读状态
        yxyNoticeOutter.setReceiveSchool(yxyNotifictionSchooLst);
        yxyNoticeOutter = yxyNoticeOutterRepository.save(yxyNoticeOutter);

        RespModel respModel = new RespModel();
        respModel.setCurrentSchoolId(currentSchoolId);
        // 详情不需要显示发送的教育局信息
        respModel.setCurrentSchoolNick("");
        respModel.setCurrentUserId(currentUserId);
        respModel.setCurrentUserNickname(yxyNoticeOutter.getUserNick());
        respModel.setYxyNoticeOutter(yxyNoticeOutter);
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 内部通知id
        public String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

    }

    protected static class RespModel {

        // 内部通知详细信息
        private YxyNoticeOutter yxyNoticeOutter;

        // 当前用户id
        private String currentUserId;

        // 当前用户昵称
        private String currentUserNickname;

        // 当前用户教育局id
        private String currentSchoolId;

        // 当前用户教育局昵称
        private String currentSchoolNick;

        public YxyNoticeOutter getYxyNoticeOutter() {
            return yxyNoticeOutter;
        }

        public void setYxyNoticeOutter(YxyNoticeOutter yxyNoticeOutter) {
            this.yxyNoticeOutter = yxyNoticeOutter;
        }

        public String getCurrentUserId() {
            return currentUserId;
        }

        public void setCurrentUserId(String currentUserId) {
            this.currentUserId = currentUserId;
        }

        public String getCurrentUserNickname() {
            return currentUserNickname;
        }

        public void setCurrentUserNickname(String currentUserNickname) {
            this.currentUserNickname = currentUserNickname;
        }

        public String getCurrentSchoolId() {
            return currentSchoolId;
        }

        public void setCurrentSchoolId(String currentSchoolId) {
            this.currentSchoolId = currentSchoolId;
        }

        public String getCurrentSchoolNick() {
            return currentSchoolNick;
        }

        public void setCurrentSchoolNick(String currentSchoolNick) {
            this.currentSchoolNick = currentSchoolNick;
        }
    }
}
